package com.deepinjava.dbbmlite;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.deepinjava.dbbmlite.Application.*;

/**
 * 数据库访问帮助类
 *
 * @author administrator
 *
 */
public class JdbcHelper {

    private Connection conn = null;
    private PreparedStatement preparedStatement = null;
    private CallableStatement callableStatement = null;
    private Statement statement = null;

    public JdbcHelper() throws SQLException {
        conn = DriverManager.getConnection(
                TASK_PROP.getProperty("url"),
                TASK_PROP.getProperty("username"),
                TASK_PROP.getProperty("password"));
        conn.setAutoCommit(false);
//        System.out.println(conn);
//        statement = conn.createStatement();
//        statement= conn.
    }

    /**
     * 用于查询，返回结果集
     *
     * @param sql
     *            sql语句
     * @return 结果集
     * @throws SQLException
     */
    @SuppressWarnings("rawtypes")
    public List query(String sql) throws SQLException {

        ResultSet rs = null;
        try {
            getPreparedStatement(sql);
            rs = preparedStatement.executeQuery();

            return ResultToListMap(rs);
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }

    }

    /**
     * 用于带参数的查询，返回结果集
     *
     * @param sql
     *            sql语句
     * @param paramters
     *            参数集合
     * @return 结果集
     * @throws SQLException
     */
    @SuppressWarnings("rawtypes")
    public List query(String sql, Object... paramters)
            throws SQLException {

        ResultSet rs = null;
        try {
            getPreparedStatement(sql);

            for (int i = 0; i < paramters.length; i++) {
                preparedStatement.setObject(i + 1, paramters[i]);
            }
            rs = preparedStatement.executeQuery();
            return ResultToListMap(rs);
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }
    }

    /**
     * 返回单个结果的值，如count\min\max等等
     *
     * @param sql
     *            sql语句
     * @return 结果集
     * @throws SQLException
     */
    public  Object getSingle(String sql) throws SQLException {
        Object result = null;
        ResultSet rs = null;
        try {
            getPreparedStatement(sql);
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                result = rs.getObject(1);
            }
            return result;
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }

    }

    /**
     * 返回单个结果值，如count\min\max等
     *
     * @param sql
     *            sql语句
     * @param paramters
     *            参数列表
     * @return 结果
     * @throws SQLException
     */
    public  Object getSingle(String sql, Object... paramters)
            throws SQLException {
        Object result = null;
        ResultSet rs = null;
        try {
            getPreparedStatement(sql);

            for (int i = 0; i < paramters.length; i++) {
                preparedStatement.setObject(i + 1, paramters[i]);
            }
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                result = rs.getObject(1);
            }
            return result;
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }
    }

    /**
     * 用于增删改
     *
     * @param sql
     *            sql语句
     * @return 影响行数
     * @throws SQLException
     */
    public  int update(String sql) throws SQLException {

        try {
            getPreparedStatement(sql);
//        System.out.println("at update:"+ conn);
//        statement = conn.createStatement();

            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw e;
        } finally {
            preparedStatement.close();
        }
    }

    /**
     * 用于增删改（带参数）
     *
     * @param sql
     *            sql语句
     * @param paramters
     *            sql语句
     * @return 影响行数
     * @throws SQLException
     */
    public  int update(String sql, Object... paramters)
            throws SQLException {
        try {
            getPreparedStatement(sql);

            for (int i = 0; i < paramters.length; i++) {
                preparedStatement.setObject(i + 1, paramters[i]);
            }
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            preparedStatement.close();
        }
    }

    /**
     * 插入值后返回主键值
     *
     * @param sql
     *            插入sql语句
     * @return 返回结果
     * @throws Exception
     */
    public  Object insertWithReturnPrimeKey(String sql)
            throws SQLException {
        ResultSet rs = null;
        Object result = null;
        try {
            preparedStatement = conn.prepareStatement(sql,
                    PreparedStatement.RETURN_GENERATED_KEYS);
            preparedStatement.execute();
            rs = preparedStatement.getGeneratedKeys();
            if (rs.next()) {
                result = rs.getObject(1);
            }
            return result;
        } catch (SQLException e) {
            throw new SQLException(e);
        }
    }

    /**
     * 插入值后返回主键值
     *
     * @param sql
     *            插入sql语句
     * @param paramters
     *            参数列表
     * @return 返回结果
     * @throws SQLException
     */
    public  Object insertWithReturnPrimeKey(String sql,
                                                  Object... paramters) throws SQLException {
        ResultSet rs = null;
        Object result = null;
        try {
           preparedStatement = conn.prepareStatement(sql,
                    PreparedStatement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < paramters.length; i++) {
                preparedStatement.setObject(i + 1, paramters[i]);
            }
            preparedStatement.execute();
            rs = preparedStatement.getGeneratedKeys();
            if (rs.next()) {
                result = rs.getObject(1);
            }
            return result;
        } catch (SQLException e) {
            throw new SQLException(e);
        }

    }

    /**
     * 调用存储过程执行查询
     *
     * @param procedureSql
     *            存储过程
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("rawtypes")
    public  List callableQuery(String procedureSql) throws SQLException {
        ResultSet rs = null;
        try {
            getCallableStatement(procedureSql);
            rs = callableStatement.executeQuery();
            return ResultToListMap(rs);
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }
    }

    /**
     * 调用存储过程（带参数）,执行查询
     *
     * @param procedureSql
     *            存储过程
     * @param paramters
     *            参数表
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("rawtypes")
    public  List callableQuery(String procedureSql, Object... paramters)
            throws SQLException {
        ResultSet rs = null;
        try {
            getCallableStatement(procedureSql);

            for (int i = 0; i < paramters.length; i++) {
                callableStatement.setObject(i + 1, paramters[i]);
            }
            rs = callableStatement.executeQuery();
            return ResultToListMap(rs);
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }
    }

    /**
     * 调用存储过程，查询单个值
     *
     * @param procedureSql
     * @return
     * @throws SQLException
     */
    public  Object callableGetSingle(String procedureSql)
            throws SQLException {
        Object result = null;
        ResultSet rs = null;
        try {
            getCallableStatement(procedureSql);
            rs = callableStatement.executeQuery();
            while (rs.next()) {
                result = rs.getObject(1);
            }
            return result;
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }
    }

    /**
     * 调用存储过程(带参数)，查询单个值
     *
     * @param procedureSql
     * @param paramters
     * @return
     * @throws SQLException
     */
    public  Object callableGetSingle(String procedureSql,
                                           Object... paramters) throws SQLException {
        Object result = null;
        ResultSet rs = null;
        try {
            getCallableStatement(procedureSql);

            for (int i = 0; i < paramters.length; i++) {
                callableStatement.setObject(i + 1, paramters[i]);
            }
            rs = callableStatement.executeQuery();
            while (rs.next()) {
                result = rs.getObject(1);
            }
            return result;
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            free(rs);
        }
    }

    public  Object callableWithParamters(String procedureSql)
            throws SQLException {
        try {
            getCallableStatement(procedureSql);
            callableStatement.registerOutParameter(0, Types.OTHER);
            callableStatement.execute();
            return callableStatement.getObject(0);

        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            callableStatement.close();
        }

    }

    /**
     * 调用存储过程，执行增删改
     *
     * @param procedureSql
     *            存储过程
     * @return 影响行数
     * @throws SQLException
     */
    public  int callableUpdate(String procedureSql) throws SQLException {
        try {
            getCallableStatement(procedureSql);
            return callableStatement.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            callableStatement.close();
        }
    }

    /**
     * 调用存储过程（带参数），执行增删改
     *
     * @param procedureSql
     *            存储过程
     * @param parameters
     * @return 影响行数
     * @throws SQLException
     */
    public  int callableUpdate(String procedureSql, Object... parameters)
            throws SQLException {
        try {
            getCallableStatement(procedureSql);
            for (int i = 0; i < parameters.length; i++) {
                callableStatement.setObject(i + 1, parameters[i]);
            }
            return callableStatement.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            callableStatement.close();
        }
    }

    /**
     * 批量更新数据
     *
     * @param sqlList
     *            一组sql
     * @return
     */
    public  int[] batchUpdate(List<String> sqlList) throws SQLException {

        int[] result = new int[] {};
        Statement statenent = null;
        try {
            statenent = conn.createStatement();
            for (String sql : sqlList) {
                statenent.addBatch(sql);
            }
            result = statenent.executeBatch();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                throw new ExceptionInInitializerError(e1);
            }
            throw new ExceptionInInitializerError(e);
        } finally {
            statenent.close();
        }
        return result;
    }

    public  int[] batchUpdate(List<String> sqlList,List<List> paramterlist) throws SQLException {

        int[] result = new int[] {};
//        prepStatem = conn.prepareStatement()

        try {
            for (int i = 0; i < sqlList.size(); i++) {
                getPreparedStatement(sqlList.get(i));
                for (int j = 0; j < paramterlist.get(i).size(); j++){
                    preparedStatement.setObject(j + 1, paramterlist.get(i).get(j));
                }
                preparedStatement.addBatch();
            }
            result = preparedStatement.executeBatch();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                throw new ExceptionInInitializerError(e1);
            }
            throw new ExceptionInInitializerError(e);
        } finally {
            preparedStatement.close();
        }
        return result;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private  List ResultToListMap(ResultSet rs) throws SQLException {
        List list = new ArrayList();
        while (rs.next()) {
            ResultSetMetaData md = rs.getMetaData();
            Map map = new HashMap();
            for (int i = 1; i < md.getColumnCount()+1; i++) {
                map.put(md.getColumnLabel(i), rs.getObject(i));
            }
            list.add(map);
        }
        return list;
    }

    /**
     * 获取PreparedStatement
     *
     * @param sql
     * @throws SQLException
     */
    private  void getPreparedStatement(String sql) throws SQLException {
        preparedStatement = conn.prepareStatement(sql);
    }

    /**
     * 获取CallableStatement
     *
     * @param procedureSql
     * @throws SQLException
     */
    private  void getCallableStatement(String procedureSql)
            throws SQLException {
        callableStatement = conn.prepareCall(procedureSql);
    }

    public void commit() throws SQLException {
        conn.commit();
    }

    public void rollback() throws SQLException {
        conn.rollback();
    }

    /**
     * 释放资源
     *
     * @param rs
     *            结果集
     */
    public  void free(ResultSet rs) throws SQLException {

        rs.close();
        preparedStatement.close();

//        JdbcUnits.free(conn, preparedStatement, rs);
    }

    /**
     * 释放资源
     *
     * @param statement
     * @param rs
     */
    public  void free(Statement statement, ResultSet rs) throws SQLException {

        rs.close();
        statement.close();
    }

}

